/**
 * @Title: ErFenSearch.java
 * @Package com.yudylaw.algorithm
 * @Description: TODO
 * @author yl76704
 * @date Jul 25, 2011
 * @version V1.0 
 */
package com.yudylaw.algorithm;

/**
 * @ClassName: ErFenSearch
 * @Description: TODO
 * @author yl76704
 * @date Jul 25, 2011
 *
 */
public class ErFenSearch {

	/**
	 * @Title: main
	 * @Description: TODO
	 * @param args void 
	 * @throws
	 * @Author: yl76704
	 * @CreateDate: Jul 25, 2011
	 * @Version : V1.0
	 */
	public static void main(String[] args) {
		int[] nums = {12,2,35,33,555,33,0,103,1023,1003,2333,290};
		KuaiSuSort.quickSort(nums, 0, nums.length-1);
		
		int goal =  ErFenSearch.search(nums, 555, 0, nums.length-1);
		System.out.println("goal is : "+goal);
	}

	/**
	 * nums is a sorted array
	 * small - > large
	 */
	public static int search(int [] nums,int goal,int begin ,int end){
		if(begin>end){
			return -1;
		}else if(begin==end){//only two elements
			if(nums[begin]==goal)return goal;
			return -1;
		}
		int m = (begin + end) /2;
		if(nums[m]>goal)
			return search(nums,goal,begin,m-1);
		else if(nums[m]<goal)
			return search(nums,goal,m+1,end);
		else return nums[m];
	}
}
